package com.zplus.managers.petservices.controllers;

import com.alibaba.fastjson2.JSON;
import com.zplus.managers.petservices.entities.Admin;
import com.zplus.managers.petservices.services.AdminService;
import com.zplus.managers.petservices.utils.Message;
import com.zplus.managers.petservices.utils.Page;
import com.zplus.managers.petservices.utils.PagingFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/admin")
public class AdminController
{
    @Autowired
    private AdminService adService;
    private PagingFactory pagingFactory;
    /**
     * check user login
     * require:
     * phoneNum or email
     * @param admin admin object
     * @return Message object with operation resul
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    Message login(@RequestBody Admin admin, HttpServletResponse response, HttpServletRequest request)
    {
        Message msg = new Message();
        Admin ad = adService.checkLogin(admin);

        if(ad != null)
        {
            ad.setPasswd("");
            msg.ok();
            String json = URLEncoder.encode(JSON.toJSONString(ad), StandardCharsets.UTF_8);
            Cookie coo = new Cookie("adminInfo", json);
            response.addCookie(coo);
        }
        else
            msg.fail();
        return msg;
    }

    /**
     * search admin user by given id
     * @param aid admin id
     * @return admin object if success, emtpy content otherwise
     */
    @RequestMapping(value = "/getadminbyid", method = RequestMethod.GET)
    @ResponseBody
    Message searchAdmin(@RequestParam("aid") int aid)
    {
        Message msg = new Message();
        Admin adminById = adService.getAdminById(aid);
        if(adminById != null)
        {
            msg.ok();
            Map<String, Admin> res = new HashMap<>();
            res.put(String.valueOf(adminById.getId()), adminById);
            msg.setContents(res);
        }
        else
            msg.fail();
        return msg;
    }

    /**
     * get page
     * @param index index of
     * @return Messge with first page
     */
    @RequestMapping(value = "/getpage", method = RequestMethod.GET)
    @ResponseBody
    Message getPage(@RequestParam("index") int index)
    {
        Message msg = new Message();
        if(pagingFactory == null)
        {
            msg.fail("paggingFactory not initialized");
            return msg;
        }

        Page page = pagingFactory.getPage(index);
        msg.loadPage(page);
        return msg;
    }

    /**
     * get all admins;
     * require a get parametter: pageSize
     * @param pageSize the num of items that a page can hold
     * @return Message contains first page with code 200 if success, code 200 with contents otherwise
     */
    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    @ResponseBody
    Message getALlAdmin(@RequestParam("pageSize") int pageSize)
    {
        Message msg = new Message();
        List<Admin> admins = adService.getAllAdmin();
        if(admins == null || admins.isEmpty())
        {
            msg.fail();
            return msg;
        }

        pagingFactory = new PagingFactory(admins, pageSize);
        Page page = pagingFactory.nextPage();
        msg.loadPage(page);
        return msg;
    }

    /**
     * search admin by given nickname
     * require: nickname
     * example: /admin/getbyname?nickname=aaa  <- search for admin 'aaa'
     * @param nickname the nickname of an admin
     * @return message with content list that hold admin object
     */
    @RequestMapping(value = "/getbyname", method = RequestMethod.GET)
    @ResponseBody
    Message getAdminByName(@RequestParam("nickname")String nickname)
    {
        Message msg = new Message();
        Admin adminByNickname = adService.getAdminByNickname(nickname);
        if(adminByNickname == null)
        {
            msg.fail();
            return msg;
        }
        List<Admin> tmp = new ArrayList<>();
        tmp.add(adminByNickname);
        msg.load(tmp);
        return msg;
    }
}